package com.masterclient.app.modules.main.widget;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.main.adapter.ScanWelfareFailureAdapter;
import com.masterclient.app.modules.main.beans.ScanWelfareErrorBean;
import com.masterclient.app.modules.main.manager.ScanManager;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/7/27.
 */
public class ScanWelfareFailureDialog extends Dialog {

    @BindView(R.id.dialog_scan_failure_close)
    ImageView dialogScanFailureClose;
    @BindView(R.id.dialog_scan_failure_title)
    TextView dialogScanFailureTitle;
    @BindView(R.id.dialog_scan_failure_time)
    TextView dialogScanFailureTime;
    @BindView(R.id.dialog_scan_failure_content)
    RecyclerView dialogScanFailureContent;
    @BindView(R.id.dialog_scan_failure_content_layout)
    LinearLayout dialogScanFailureContentLayout;
    @BindView(R.id.dialog_scan_failure_desc)
    TextView dialogScanFailureDesc;
    @BindView(R.id.dialog_scan_failure_btn)
    ImageView dialogScanFailureBtn;

    private Context mContext;
    private ConfirmCallback confirmCallback;
    private ScanWelfareErrorBean dataBean;
    private String title;

    public interface ConfirmCallback {
        void onConfirm();
    }

    public ScanWelfareFailureDialog(@NonNull Context context, String title, ScanWelfareErrorBean dataBean, ConfirmCallback confirmCallback) {
        super(context);
        mContext = context;
        this.title = title;
        this.dataBean = dataBean;
        this.confirmCallback = confirmCallback;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_scan_welfare_failure);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(false);
        dialogScanFailureTitle.setText(title.replace(",","\n"));

        if (dataBean != null){
            ((LinearLayout.LayoutParams)dialogScanFailureTitle.getLayoutParams()).setMargins(
                    0,ScreenUtils.dp2px(10),0,0);
            dialogScanFailureDesc.setText(dataBean.getRemark().replace(",","\n"));
            dialogScanFailureTime.setText("领取时间："+dataBean.getTime());
            dialogScanFailureDesc.setVisibility(View.VISIBLE);
            dialogScanFailureTime.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(dataBean.getPrize())){
                List<String> strings = Arrays.asList(dataBean.getPrize().split(","));
                dialogScanFailureContentLayout.setVisibility(View.VISIBLE);
                if (strings.size()>3) {
                    dialogScanFailureContent.getLayoutParams().height = ScreenUtils.dp2px(69);
                }
                dialogScanFailureContent.setLayoutManager(new LinearLayoutManager(mContext));
                dialogScanFailureContent.addItemDecoration(new RecyclerView.ItemDecoration() {
                    @Override
                    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                        super.getItemOffsets(outRect, view, parent, state);
                        if (parent.getChildAdapterPosition(view) != strings.size()-1){
                            outRect.bottom = ScreenUtils.dp2px(2);
                        }
                    }
                });

                ScanWelfareFailureAdapter failureAdapter = new ScanWelfareFailureAdapter();
                dialogScanFailureContent.setAdapter(failureAdapter);
                failureAdapter.setNewData(strings);
            }
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth();
        windowparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(R.color.transparent);
        window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        window.setAttributes(windowparams);
    }


    @OnClick({R.id.dialog_scan_failure_close, R.id.dialog_scan_failure_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_scan_failure_close:
                dismiss();
                break;
            case R.id.dialog_scan_failure_btn:
                if (confirmCallback != null) confirmCallback.onConfirm();
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        ScanManager.getScanManager().welfareBean = null;
        ScanManager.getScanManager().code = null;;
        ScanManager.getScanManager().welfareFailuredialog = null;
    }
}
